﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WorkStation.Frameworks.Core;
using WorkStation.Frameworks.Core.Extensions;

using WorkStation.Frameworks.EasyBus;
using WorkStation.Frameworks.EasyBus.MessageQueue;
using WorkStation.Frameworks.EasyBus.MessageQueue.RabbitMq;
using WorkStation.Frameworks.EasyBus.Serializable;
using WorkStation.Frameworks.EasyBus.Extensions;
using WorkStation.Frameworks.EasyBus.Attributes;
using WorkStation.Frameworks.EasyBus.Handles;
using WorkStation.Frameworks.EasyBus.Repositories.Mongo;
using WorkStation.Frameworks.EasyBus.Repositories.ZooKeeper;

using WorkStation.Frameworks.Configuration;
using WorkStation.Frameworks.ServiceContainer;

using Microsoft.Practices.ServiceLocation;

namespace WorkStation.Frameworks.EasyBus.Test.Subscribe
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigurationManager.GetConfiguration<ServiceContainerConfiguration>().InitServiceLocator();

            ServiceContainer.Current.UseServiceLocator();

            string busName = System.Configuration.ConfigurationManager.AppSettings["BusName"];
            string nodeName = System.Configuration.ConfigurationManager.AppSettings["NodeName"];

            IBus bus = EasyBus.GetInstance(busName, nodeName).SetAttributeConfigProvider("WorkStation.Frameworks.EasyBus.Test.Subscribe")
                                                             .SetDefaultErrorLogger()
                                                             .SetupServer()
                                                             .UserZooKeeper("10.32.33.140:2181", new TimeSpan(0, 0, 0, 20, 0))
                                                             .UserMongodb("mongodb://10.32.11.131")
                                                             .Start();

            Console.WriteLine("Bus:{0} begin listening".Fmt(bus.Name));
            Console.ReadLine();
        }
    }

    [Subscribe(
        BusName = "EasyTest.Publish", 
        RouteKey = "EasyBus.PublishMessage", 
        Name = "SubscribeA",
        Mq = "RabbitMq:HostName=10.32.33.140;Port=5672;VirtualHost=/;UserName=songxd;Password=123abc;QueueName=TestQueue;IsSynchronization=false")]
    [Listener(SubscribeName = "SubscribeA", HandleName = "HandleA;HandleB")]
    public class SubscribeMessage 
    {
        public string Name { get; set; }
    }

    public class TestHandle : ProcessHandle
    {
        public TestHandle() { }

        protected override IProcessHandleResult ProcessContent(BusActionContext context)
        {
            if (!System.IO.Directory.Exists(@"D:\Message\"))
            {
                System.IO.Directory.CreateDirectory(@"D:\Message\");
            }

            System.IO.File.WriteAllText(string.Format(@"D:\Message\{0}_{1}.txt", Guid.NewGuid().ToString(), this.Name), context.Input.ToJson());
            
            return new ProcessSuccessResult(context.Input, this, this.Listener.Endpoint);
        }
    }
}
